//
// Created by Chen qin lang on 2018/7/6.
//

#ifndef CQAUDIO_WLAUDIO_H
#define CQAUDIO_WLAUDIO_H

#include "pthread.h"
#include "JavaCall.h"
#include <android/log.h>
#include "WlAudio.h"
#include "WlQueue.h"
#include "SLES/OpenSLES.h"
#include "SLES/OpenSLES_Android.h"
#include "SoundTouch.h"
using  namespace soundtouch;

extern "C"{
#include <libswresample/swresample.h>
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include <libavutil/time.h>
};


class WlAudio {
public:
   int streamIndex=-1;
    AVCodecParameters* codecpar;
    AVCodecContext *codecCtx;
    WlQueue *queue;
    pthread_t play_thread;
    uint8_t *buffer;
    int  dataSize;
    WlPlayStat *playStat;
    int sampleRate;
    JavaCall *call;
    int duration;
    AVRational timebase;
    double  now_time;
    double clock;//参考时间,当pts未设置时以这个为准
    double last_time;
    int volumeProcess;//el声音大小

    // 引擎接口
    SLObjectItf engineObject = NULL;
    SLEngineItf engineEngine = NULL;

    //混音器
    SLObjectItf outputMixObject = NULL;
    SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;
    SLEnvironmentalReverbSettings reverbSettings = SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR;

    //pcm
    SLObjectItf pcmPlayerObject = NULL;
    SLPlayItf pcmPlayerPlay = NULL;
    SLVolumeItf pcmPlayerVolume = NULL;
    SLMuteSoloItf pcmPlayerMute=NULL;
    //缓冲器队列接口
    SLAndroidSimpleBufferQueueItf pcmBufferQueue = NULL;

    SoundTouch *soundTouch=NULL;
    SAMPLETYPE *sampleBuff=NULL;
    uint8_t  *out_buffer;
    int nb;// 采样个数
    int num;//soundtouch采样个数
    bool  finished=true;

public:
    WlAudio( int streamIndex, WlPlayStat * playStat,int sampleRate,  JavaCall *call);
    ~WlAudio();
    void initOPELES();
    void play();

    int resampleAudio(void** out_buffer);
    SLuint32 getSLSampleRate();
    void pause();
    void resume();
    void stop();
    void release();

    void setVolume(int process);

    void setMuter(int i);
    int getSoundData();

    void setPitch(float pitch);

    void setTempo(float tempo);
};


#endif //CQAUDIO_WLAUDIO_H
